package barcode.android.dao.impl;

import java.util.Calendar;
import java.util.Date;

import junit.framework.Assert;
import barcode.android.activity.Barcode_androidActivity;
import barcode.android.dao.MessageDao;
import barcode.android.dao.impl.BaseDaoImpl;
import barcode.android.dao.impl.SqliteDatabaseState;
import barcode.android.model.Message;
import barcode.android.model.MessageStatus;
import barcode.android.model.Organization;
import barcode.android.model.Unit;
import barcode.android.model.UserInfo;
import android.test.ActivityInstrumentationTestCase2;

public class MessageDaoTest extends
		ActivityInstrumentationTestCase2<Barcode_androidActivity> {

	public MessageDaoTest() {
		super(Barcode_androidActivity.class);
	}

	public void setUp() {
		BaseDaoImpl.initContext(getActivity().getBaseContext());
	}

	public void testAddMessage() {
		Message msg = new Message();
		msg.setContent("测试消息");
		msg.setStatus(MessageStatus.UnReaded);
		Unit sender = new UserInfo();
		sender.setId(4);
		Unit receiver = new Organization();
		receiver.setId(2);
		msg.setSender(sender);
		msg.setReceiver(receiver);
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		msg.setTime(cal);

		MessageDao dao = new MessageDaoImpl();
		boolean res = false;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.add(msg);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(true, res);
	}

	public void testQueryMessage() {
		MessageDao dao = new MessageDaoImpl();
		Message msg = null;
		try {
			dao.beginTransaction(SqliteDatabaseState.Readable);
			msg = dao.query(5);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertNotNull(msg);
		Assert.assertEquals("测试消息", msg.getContent());
		Assert.assertEquals(MessageStatus.UnReaded, msg.getStatus());
		Assert.assertEquals(4, msg.getSender().getId());
		Assert.assertEquals(2, msg.getReceiver().getId());
	}

	public void testDeleteMessage() {
		MessageDao dao = new MessageDaoImpl();
		boolean res = false;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.delete(3);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(true, res);
	}

	public void testUpdateMessage() {
		Message msg = new Message();
		msg.setId(5);
		msg.setContent("test message");
		msg.setStatus(MessageStatus.Readed);
		Unit sender = new UserInfo();
		sender.setId(4);
		Unit receiver = new Organization();
		receiver.setId(3);
		msg.setSender(sender);
		msg.setReceiver(receiver);
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		msg.setTime(cal);

		MessageDao dao = new MessageDaoImpl();
		boolean res = false;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.update(msg);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(true, res);

		try {
			dao.beginTransaction(SqliteDatabaseState.Readable);
			msg = dao.query(5);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertNotNull(msg);
		Assert.assertEquals("test message", msg.getContent());
		Assert.assertEquals(MessageStatus.Readed, msg.getStatus());
		Assert.assertEquals(4, msg.getSender().getId());
		Assert.assertEquals(3, msg.getReceiver().getId());
	}
}
